package cn.gxa.j229.bug.finance.controller.borrowing;

import cn.gxa.j229.bug.finance.pojo.InvestmentRecord;
import cn.gxa.j229.bug.finance.pojo.Loan;
import cn.gxa.j229.bug.finance.service.borrowing.AllBorrowingSignService;
import cn.gxa.j229.bug.finance.utils.JsonResult;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;

/**
 * 所有借款标页面
 * @Author FanJiang
 * @Date 2020/9/28
 * @Version 1.0
 */

@Controller
@RequestMapping("/allBorSign")
public class AllBorrowingSignController {

    @Resource
    AllBorrowingSignService allBorrowingSignService;

    /**
     * 所有借款标页面接口
     * @param loan
     * @return
     */
    @PostMapping("/findAllBorSignPage")
    @ResponseBody
    public Map findAll(@RequestBody Loan loan) {
        List allPage = allBorrowingSignService.findAllPage(loan);
        Integer counts = allBorrowingSignService.countPage(loan);
        JsonResult jsonResult = new JsonResult();
        jsonResult.put("count", counts);
        jsonResult.setData(allPage);
        jsonResult.setCode("200");
        jsonResult.setMsg("成功");
        return jsonResult.getValues();

    }

    /**
     * 所有借款标页面
     * 投资记录操作
     * @param investmentRecord
     * @return
     */
    @PostMapping("/findByNum")
    @ResponseBody
    public Map findByNum(@RequestBody InvestmentRecord investmentRecord) {
        List byNum = allBorrowingSignService.findByNum(investmentRecord.getNum());
        JsonResult jsonResult = new JsonResult();
        jsonResult.setData(byNum);
        jsonResult.setCode("200");
        jsonResult.setMsg("成功");
        return jsonResult.getValues();

    }

    //导出表格
    //目前数据仅能写死
    @GetMapping("/export")
    @ResponseBody
    public Map export(HttpServletResponse resp) {
        try {
            //2.创建XSSFWorkbook
            XSSFWorkbook workbook = new XSSFWorkbook();
//            Loan loan = new Loan();
//            loan.setPhone(phone);
//            loan.setBorrower(borrower);
//            loan.setState(Integer.valueOf(state));
            allBorrowingSignService.export(workbook);

            //告诉浏览器以下载的方式代开
            resp.setHeader("Content-Disposition", "attachment;filename=" +
                    URLEncoder.encode("所有借款标表.xlsx", "UTF-8"));
            ServletOutputStream out = resp.getOutputStream();
            workbook.write(out);

            JsonResult jsonResult = new JsonResult();
            jsonResult.setCode("200");
            jsonResult.setMsg("导出表格成功");

            return jsonResult.getValues();
        } catch (Exception e) {
            e.printStackTrace();
            JsonResult jsonResult = new JsonResult();
            jsonResult.setCode("50050");
            jsonResult.setMsg("导出表格失败");

            return jsonResult.getValues();
        }

    }

}
